package com.lhkj.ct.admin.modules.common.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.github.xiaoymin.knife4j.annotations.ApiSupport;
import com.lhkj.ct.base.enums.DeleteStatus;
import com.lhkj.ct.base.enums.PatSourceEnum;
import com.lhkj.ct.base.model.ro.ActionResult;
import com.lhkj.ct.base.utils.ShiroUtil;
import com.lhkj.ct.meta.modules.patient.mapper.PatUnitMapper;
import com.lhkj.ct.meta.modules.patient.mapper.mapper.TblUnitTaskMapper;
import com.lhkj.ct.meta.modules.patient.model.entity.TblUnitTask;
import com.lhkj.ct.meta.modules.patient.service.PatSourceService;
import com.lhkj.ct.meta.modules.psychology.mapper.ScaleReportMapper;
import com.lhkj.ct.meta.modules.psychology.model.entity.TblScalePsyRecord;
import com.lhkj.ct.meta.modules.psychology.model.entity.TblScaleReport;
import com.lhkj.ct.meta.modules.psychology.service.PsyRecordService;
import com.lhkj.ct.meta.modules.scheduling.model.dto.ScheduleSearch;
import com.lhkj.ct.meta.modules.scheduling.model.entity.TblScheduling;
import com.lhkj.ct.meta.modules.scheduling.service.SchedulingService;
import com.lhkj.ct.meta.modules.warning.mapper.ScaleWarnRecordMapper;
import com.lhkj.ct.meta.modules.warning.model.entity.TblScaleWarnRecord;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
  * <p>
  *     描述：首页控制器
  * </p>
  */
@RestController
@RequestMapping("api/system/home")
@Api(tags = "1.首页")
@ApiSupport(order = 0)
@Validated
public class HomePageController {

    @Resource
    private PsyRecordService psyRecordService;

    @Resource
    private PatSourceService patSourceService;

    @Resource
    private SchedulingService schedulingService;

    @Resource
    private ScaleWarnRecordMapper scaleWarnRecordMapper;

    @Resource
    private ScaleReportMapper scaleReportMapper;

    @Resource
    private TblUnitTaskMapper unitTaskMapper;

    @Resource
    private PatUnitMapper patUnitMapper;


    /**
     * 今日分配量表，今日预警统计，待审核报告量，单位任务提醒
     */
    @ApiOperationSupport(order = 1)
    @ApiOperation(value = "今日统计", notes = "1.量表分发统计，2.今日预警统计，3.待审核报告量，4.单位任务提醒")
    @GetMapping("today")
    public ActionResult<Map<String, Long>> list() {
        LocalDateTime now = LocalDateTime.now();
        Long count1 = psyRecordService.getBaseMapper().selectCount(new LambdaQueryWrapper<TblScalePsyRecord>()
                .eq(TblScalePsyRecord::getDelFlag, DeleteStatus.NORMAL)
        );
        Long count2 = scaleWarnRecordMapper.selectCount(new LambdaQueryWrapper<TblScaleWarnRecord>()
                .between(TblScaleWarnRecord::getWarnTime, LocalDate.now().atStartOfDay(), now));
        Long count3 = scaleReportMapper.selectCount(new LambdaQueryWrapper<TblScaleReport>()
                .eq(TblScaleReport::getReportStatus, 0)
                .eq(TblScaleReport::getDelFlag, DeleteStatus.NORMAL));
        Long count4 = unitTaskMapper.selectCount(new LambdaQueryWrapper<TblUnitTask>()
                .ge(TblUnitTask::getReminderDate, LocalDate.now()));
        Map<String, Long> map = new HashMap<>();
        map.put("count1", count1);
        map.put("count2", count2);
        map.put("count3", count3);
        map.put("count4", count4);
        return ActionResult.success(map);
    }

    @ApiOperationSupport(order = 2)
    @ApiOperation(value = "我的排班")
    @GetMapping("scheduling")
    public ActionResult<List<TblScheduling>> scheduling() {
        return ActionResult.success(schedulingService.selectScheTimes(new ScheduleSearch(LocalDate.now(), ShiroUtil.getAuthUserInfo().getUserId())));
    }

    @ApiOperationSupport(order = 3)
    @ApiOperation(value = "档案来源")
    @GetMapping("patSource")
    public ActionResult<List<Map<String, Object>>> patSource() {
        return ActionResult.success(patSourceService.countBySourceType(PatSourceEnum.values()));
    }

    @ApiOperationSupport(order = 4)
    @ApiOperation(value = "最近一周分配量")
    @GetMapping("lastWeek")
    public ActionResult<List<Map<String, Object>>> lastWeek() {
        return ActionResult.success(psyRecordService.countGroupByDate(LocalDate.now().minusDays(6), LocalDate.now()));
    }

    @ApiOperationSupport(order = 5)
    @ApiOperation(value = "根据单位统计", notes = "countPatient:档案数量，countAllot:分配数量，countWarn:预警数量")
    @GetMapping("countByUnit")
    public ActionResult<List<Map<String, Long>>> countByUnit(@RequestParam(value = "id", required = false) String id) {
        return ActionResult.success(patUnitMapper.statisticsByUnit(id));
    }
}
